//
//  CarBasicView.h
//  UcarTest
//
//  Created by 陈曦 on 16/3/19.
//  Copyright © 2016年 chenxi. All rights reserved.
//

#import "GBTagListView.h"
#define HORIZONTAL_PADDING 7.0f
#define VERTICAL_PADDING   2.0f
#define LABEL_MARGIN       10.0f
#define BOTTOM_MARGIN      10.0f
#define KTapTag            1000
@implementation GBTagListView
-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        totalHeight = 0;
        self.frame = frame;
    }
    return self;
    
    
}
-(void)setTagWithTagArray:(NSArray*)arr{
    
    previousFrame = CGRectZero;
    [arr enumerateObjectsUsingBlock:^(NSString*str, NSUInteger idx, BOOL *stop) {
        
        self.tagLabel=[[UILabel alloc]initWithFrame:CGRectZero];
        
//        if(_signalTagColor){
            //可以单一设置tag的颜色
            self.tagLabel.backgroundColor = [UIColor orangeColor];
//        }else{
//            //tag颜色多样
//            self.tagLabel.backgroundColor=[UIColor colorWithRed:random()%255/255.0 green:random()%255/255.0 blue:random()%255/255.0 alpha:1];
//        }
        self.tagLabel.textAlignment = NSTextAlignmentCenter;
        self.tagLabel.textColor = [UIColor whiteColor];
        self.tagLabel.font = [UIFont boldSystemFontOfSize:14];
        self.tagLabel.text = str;
        self.tagLabel.tag = KTapTag+idx;
        self.tagLabel.layer.cornerRadius = 5;
        self.tagLabel.clipsToBounds = YES;
        NSDictionary *attrs = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:15]};
        CGSize Size_str = [str sizeWithAttributes:attrs];
        Size_str.width += HORIZONTAL_PADDING*3;
        Size_str.height += VERTICAL_PADDING*3;
        
        CGRect newRect = CGRectZero;
        
        if (previousFrame.origin.x + previousFrame.size.width + Size_str.width + LABEL_MARGIN > self.bounds.size.width) {
            
            newRect.origin = CGPointMake(10, previousFrame.origin.y + Size_str.height + BOTTOM_MARGIN);
            totalHeight += Size_str.height + BOTTOM_MARGIN;
        }
        else {
            newRect.origin = CGPointMake(previousFrame.origin.x + previousFrame.size.width + LABEL_MARGIN, previousFrame.origin.y);
            
        }
        newRect.size = Size_str;
        [self.tagLabel setFrame:newRect];
        previousFrame=self.tagLabel.frame;
        [self setHight:self andHight:totalHeight+Size_str.height + BOTTOM_MARGIN];
        [self addSubview:self.tagLabel];
       
        
    }
    
     ];
    if(_GBbackgroundColor){
        
        self.backgroundColor=_GBbackgroundColor;
        
    }else{
        
        self.backgroundColor=[UIColor whiteColor];
        
    }
    
    
}
#pragma mark-改变控件高度
- (void)setHight:(UIView *)view andHight:(CGFloat)hight
{
    CGRect tempFrame = view.frame;
    tempFrame.size.height = hight;
    view.frame = tempFrame;
}

@end
